using UnityEngine;
using DG.Tweening;

/// <summary>
/// 抛物线动画测试器 - 用于测试和演示阳光的抛物线动画效果
/// </summary>
public class ParabolicAnimationTester : MonoBehaviour
{
    [Header("测试设置")]
    [Tooltip("阳光预制体")]
    public GameObject SunPrefab;
    
    [Tooltip("测试起始位置")]
    public Vector3 StartPosition = new Vector3(0, 10, 0);
    
    [Tooltip("测试结束位置")]
    public Vector3 EndPosition = new Vector3(5, 0, 0);
    
    [Tooltip("抛物线高度")]
    public float ParabolaHeight = 5f;
    
    [Tooltip("抛物线水平偏移")]
    public float ParabolaHorizontalOffset = 2f;
    
    [Tooltip("动画持续时间")]
    public float AnimationDuration = 3f;
    
    [Tooltip("控制点数量")]
    [Range(3, 20)]
    public int ControlPoints = 8;
    
    [Header("调试设置")]
    [Tooltip("显示路径")]
    public bool ShowPath = true;
    
    [Tooltip("路径颜色")]
    public Color PathColor = Color.yellow;
    
    [Tooltip("控制点颜色")]
    public Color ControlPointColor = Color.red;
    
    private Vector3[] m_PathPoints;
    
    /// <summary>
    /// 开始测试抛物线动画
    /// </summary>
    [ContextMenu("测试抛物线动画")]
    public void TestParabolicAnimation()
    {
        if (SunPrefab == null)
        {
            Logger.LogError("请设置阳光预制体！");
            return;
        }
        
        // 创建阳光对象
        GameObject sunObject = Instantiate(SunPrefab, StartPosition, Quaternion.identity);
        
        // 计算抛物线路径
        m_PathPoints = CalculateParabolicPath(StartPosition, EndPosition);
        
        // 使用DOTween播放抛物线动画
        sunObject.transform.DOPath(m_PathPoints, AnimationDuration, PathType.CatmullRom)
            .SetEase(Ease.OutQuad)
            .OnComplete(() => {
                Logger.Log("抛物线动画完成！");
                // 可以在这里添加收集逻辑
            });
    }
    
    /// <summary>
    /// 计算抛物线路径
    /// </summary>
    private Vector3[] CalculateParabolicPath(Vector3 startPos, Vector3 endPos)
    {
        Vector3[] pathPoints = new Vector3[ControlPoints];
        
        // 计算抛物线顶点
        Vector3 midPoint = Vector3.Lerp(startPos, endPos, 0.5f);
        Vector3 peakPoint = midPoint + Vector3.up * ParabolaHeight;
        
        // 添加水平偏移
        Vector3 direction = (endPos - startPos).normalized;
        Vector3 rightDirection = Vector3.Cross(direction, Vector3.up);
        peakPoint += rightDirection * ParabolaHorizontalOffset;
        
        // 使用Catmull-Rom样条曲线生成平滑路径
        for (int i = 0; i < ControlPoints; i++)
        {
            float t = (float)i / (ControlPoints - 1);
            pathPoints[i] = CalculateCatmullRomPoint(startPos, peakPoint, endPos, t);
        }
        
        return pathPoints;
    }
    
    /// <summary>
    /// 计算Catmull-Rom样条曲线上的点
    /// </summary>
    private Vector3 CalculateCatmullRomPoint(Vector3 p0, Vector3 p1, Vector3 p2, float t)
    {
        // 简化的Catmull-Rom样条曲线计算
        // 使用二次贝塞尔曲线近似
        Vector3 p01 = Vector3.Lerp(p0, p1, t);
        Vector3 p12 = Vector3.Lerp(p1, p2, t);
        return Vector3.Lerp(p01, p12, t);
    }
    
    /// <summary>
    /// 在Scene视图中绘制路径
    /// </summary>
    private void OnDrawGizmos()
    {
        if (!ShowPath) return;
        
        // 绘制抛物线路径
        if (m_PathPoints != null && m_PathPoints.Length > 1)
        {
            Gizmos.color = PathColor;
            for (int i = 0; i < m_PathPoints.Length - 1; i++)
            {
                Gizmos.DrawLine(m_PathPoints[i], m_PathPoints[i + 1]);
            }
            
            // 绘制控制点
            Gizmos.color = ControlPointColor;
            for (int i = 0; i < m_PathPoints.Length; i++)
            {
                Gizmos.DrawWireSphere(m_PathPoints[i], 0.2f);
            }
        }
        else
        {
            // 绘制示例路径
            Vector3[] samplePath = CalculateParabolicPath(StartPosition, EndPosition);
            if (samplePath != null && samplePath.Length > 1)
            {
                Gizmos.color = PathColor;
                for (int i = 0; i < samplePath.Length - 1; i++)
                {
                    Gizmos.DrawLine(samplePath[i], samplePath[i + 1]);
                }
                
                // 绘制控制点
                Gizmos.color = ControlPointColor;
                for (int i = 0; i < samplePath.Length; i++)
                {
                    Gizmos.DrawWireSphere(samplePath[i], 0.2f);
                }
            }
        }
    }
    
    /// <summary>
    /// 重置为默认值
    /// </summary>
    [ContextMenu("重置为默认值")]
    public void ResetToDefaults()
    {
        StartPosition = new Vector3(0, 10, 0);
        EndPosition = new Vector3(5, 0, 0);
        ParabolaHeight = 5f;
        ParabolaHorizontalOffset = 2f;
        AnimationDuration = 3f;
        ControlPoints = 8;
        ShowPath = true;
        PathColor = Color.yellow;
        ControlPointColor = Color.red;
    }
}
